use crate::features::docker::container::{create_container, get_container_list, get_container_status, remove_container, start_container, stop_container};
use crate::features::docker::model::{Container, Filter};
use crate::features::rabbitmq::model::HandleStatus;
use crate::features::rabbitmq::producer::instance::send;

pub async fn display_get_container_list(handle_status: HandleStatus) {
    if let Some(ref data) = handle_status.data {
        if let Ok(data)= serde_json::from_str::<Filter>(data.as_str()) {
            match get_container_list(data).await {
                Ok(res) => {
                    let mut data = vec![];
                    res.iter().for_each(|i| {
                        data.push(Container::from_container_summary(i.clone()));
                    });
                    let handle_status= HandleStatus::from(handle_status).set_data(data);
                    send(handle_status.recv.clone().as_str(), handle_status).await;
                }
                Err(_) => {}
            };
        }
    }
}

pub async fn display_create_container(handle_status: HandleStatus) {
    if let Some(ref data) = handle_status.data {
        if let Ok(data)= serde_json::from_str::<Container>(data.as_str()) {
            match create_container(data.image.unwrap(), data.name.unwrap()).await {
                Ok(res) => {
                    let container = Container::default().set_id(res.id);
                    let handle_status= HandleStatus::from(handle_status).set_data(container);
                    send(handle_status.recv.clone().as_str(), handle_status).await;
                }
                Err(_) => {}
            };
        }
    }
}

pub async fn display_start_container(handle_status: HandleStatus) {
    if let Some(ref data) = handle_status.data {
        if let Ok(data)= serde_json::from_str::<Container>(data.as_str()) {
            match start_container(data.id.unwrap()).await {
                Ok(_) => {
                    let handle_status= HandleStatus::from(handle_status);
                    send(handle_status.recv.clone().as_str(), handle_status).await;
                }
                Err(_) => {}
            };
        }
    }
}

pub async fn display_stop_container(handle_status: HandleStatus) {
    if let Some(ref data) = handle_status.data {
        if let Ok(data)= serde_json::from_str::<Container>(data.as_str()) {
            match stop_container(data.id.unwrap()).await {
                Ok(_) => {
                    let handle_status= HandleStatus::from(handle_status);
                    send(handle_status.recv.clone().as_str(), handle_status).await;
                }
                Err(_) => {}
            }
        }
    }
}

pub async fn display_remove_container(handle_status: HandleStatus) {
    if let Some(ref data) = handle_status.data {
        if let Ok(data)= serde_json::from_str::<Container>(data.as_str()) {
            match remove_container(data.id.unwrap()).await {
                Ok(_) => {
                    let handle_status= HandleStatus::from(handle_status);
                    send(handle_status.recv.clone().as_str(), handle_status).await;
                }
                Err(_) => {
                }
            }
        }
    }
}

pub async fn display_get_container_status(handle_status: HandleStatus) {
    if let Some(ref data) = handle_status.data {
        if let Ok(data)= serde_json::from_str::<Container>(data.as_str()) {
            match get_container_status(data.id.unwrap()).await {
                Ok(res) => {
                    let container = Container::from_container_inspect_response(res);
                    let handle_status= HandleStatus::from(handle_status.clone()).set_data(container);
                    send(handle_status.recv.clone().as_str(), handle_status).await;
                }
                Err(_) => {
                }
            }
        }
    }
}